<template>
	<view class="wrapper" :class="{ 'bottom': !value}">
		<!--  #ifdef  APP-PLUS -->
		<guide v-if="!value"></guide>
		<!--  #endif -->
		<home ref="home" v-if='active==0'></home>
		<user ref="user" v-if='active==1'></user>
		<tabbar ref='commentTabbat'></tabbar>

		<!-- 版本更新弹窗 -->
		<u-popup :show="show" mode="center" :round="8" :closeOnClickOverlay="false" zIndex="11100">
			<view class="update-wrap">
				<image class="rocket" src="@/static/icon_rocket.png" mode=""></image>
				<view class="close" @click="show = false" v-if="whether == 0">
					<u-icon name="close" size="22"></u-icon>
				</view>
				<view class="update-info">
					<view class="title">检测到新版本</view>
					<view class="text">
						{{upgradeContent}}
						<!-- <view>1.首页新增优化某某某某某模块</view>
						<view>2.首页新增优化某某某某某模块</view>
						<view>3.首页新增优化某某某某某模块</view> -->
					</view>
				</view>
				<view class="update-btn" v-if="!downloading">
					<view @click="onSureClick">立即升级</view>
				</view>
				<view class="progress-box" v-else>
					<view class="progress-bg">
						<view class="progress-cover" :style="{width: progress + '%'}"></view>
					</view>
					<view class="progress-text">{{progress}}%</view>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {
		checkVersion,
		getUserMenuList
	} from '@/config/api/app.js'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import Cache from '@/utils/cache'
	import home from '@/pages/home/index.vue'
	import guide from '@/pages/home/guide.vue'
	import user from '@/pages/user/index.vue'
	import tabbar from '@/components/tabbar/tabbar.vue'
	export default {
		data() {
			return {
				active: Number || 0,

				/* 版本更新 */
				upgradeContent: '', //更新内容
				downloading: false,
				progress: 0,
				whether: 0, // 1代表强制升级
				show: false,
				value: Cache.get('LAUNCHFLAG'),
				// #ifdef APP-PLUS
				show: false,
				value: Cache.get('LAUNCHFLAG')
				// #endif
				
			}
		},
		created() {
			let token = Cache.get('TOKEN')
			// #ifdef APP-PLUS
			if (!this.value) return
			// #endif
			if (token) {
				//存在则关闭启动页进入首页
				// #ifdef APP-PLUS
				plus.navigator.closeSplashscreen();
				// #endif
			} else {
				uni.redirectTo({
					url: "/pages/login/index"
				})
			}
		},
		onLoad() {
			this.active = this.statusIndex || 0
			this.$nextTick(() => {
				this.$refs.commentTabbat.getSetting(this.active)
				this._initData();
			})
			if (Cache.get('TOKEN')) {
				// #ifdef MP-WEIXIN
				this.checkForUpdate()
				// #endif

				// #ifdef APP-PLUS
				setTimeout(() => {
					this.postCheckVersion()
				}, 1200)
				// #endif
			}
		},
		onShow() {
			if (Cache.get('TOKEN')) {
				this.getMenu()
				if (this.active == 0) {
					this.$nextTick(() => {
						this.$refs.home.getBillsWay()
						console.log(222)
					})
					if(Cache.get('selInfo') == 2){
						let a = Cache.get('stowageForm')?Cache.get('stowageForm'):''
						this.$refs.home.getStowageData(a)
					}
				}
			}
		},
		provide() {
			return {
				_mainFuncInit: this._mainFuncInit
			}
		},
		methods: {
			//查询菜单权限
			getMenu() {
				getUserMenuList().then((res) => {
					console.log(res,1111)
					if (res.code == 0) {
						Cache.set("MENU", res.data, 59 * 24 * 60 * 60);
					} else {
						uni.$u.toast(res.msg);
						uni.redirectTo({
							url: '/pages/login/index'
						})
					}
				}).catch(err => {
					console.log(err)
				})
			},
			//	检测升级
			postCheckVersion() {
				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					console.log(widgetInfo.version,'版本')
					Cache.set('version',`V${widgetInfo.version}`)
					//根据当前平台去后台检测版本号
					let obj = {
						userVersion: `V${widgetInfo.version}`,
						userPlatform: 'app'
					}
					console.log(obj,11111111111)
					checkVersion(obj).then((res) => {
						console.log(res)
						if (res.code == 0) {
							this.show = true
							this.upgradeContent = res.data.contentupdating
							this.whether = res.data.whether
						} else {
							// uni.$u.toast(res.msg);
						}
					}).catch(err => {
						console.log(err)
					})
				})
			},
			//	下载安装包
			onSureClick() {
				let platform = uni.$u.os()
				let openURL = platform === 'ios' ? '' : 'https://nfp.y2t.com/apk/shipper/运必达货主端.apk'
				this.downloading = true
				var downloadTask = uni.downloadFile({
					url: openURL,
					success: (downloadResult) => {
						console.log(downloadResult);
						if (downloadResult.statusCode === 200) {
							this.show = false
							this.downloading = false
							plus.runtime.install(downloadResult.tempFilePath, {
								force: false
							}, () => {
								plus.runtime.restart();
							}, (e) => {
								console.log(e);
								uni.$u.toast('安装升级包失败');
							});
						}
					},
					fail: () => {
						uni.$u.toast('安装包下载失败');
					}
				});
				downloadTask.onProgressUpdate((e) => {
					this.progress = e.progress
				})
			},
			checkForUpdate() {
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(res => {
					// 请求完新版本信息的回调
					console.log(res.hasUpdate);
					
				});
				updateManager.onUpdateReady(res => {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});

				});
				updateManager.onUpdateFailed(res => {
					// 新的版本下载失败
				});
			},
			_mainFuncInit() {
				this.active = this.statusIndex || 0
				if(Cache.get('TOKEN')) this.getMenu()
				console.log(this.active)
				this.$nextTick(() => {
					switch (this.active) {
						case 0:
							this.SETSTATUSINDEX(0)
							// this.$refs.home.getData();
							break;
						case 1:
							this.SETSTATUSINDEX(1)
							break;
					}
				})
			},
			_initData() {
				this._mainFuncInit();
			},
			...mapMutations(['SETSTATUSINDEX'])
		},
		computed: {
			...mapGetters(['statusIndex'])
		},
		components: {
			home,
			user,
			tabbar,
			guide
		}
	}
</script>

<style lang="scss" scoped>
	.wrapper {
		position: absolute;
		width: 100vw;
		height: 100vh;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		padding-bottom: calc(110rpx + env(safe-area-inset-bottom));
		box-sizing: border-box;
		&.bottom{
			padding-bottom: 0;
		}
	}

	.update-wrap {
		width: 540rpx;
		height: 692rpx;
		background: url('@/static/bg_update.png') no-repeat center center/contain;
		position: relative;

		.close {
			padding: 4rpx;
			position: absolute;
			top: 14rpx;
			right: 14rpx;
		}

		.rocket {
			width: 104rpx;
			height: 172rpx;
			position: absolute;
			top: -50rpx;
			left: 50%;
			transform: translate(-50%, 0);
		}

		.update-info {
			width: 70%;
			position: absolute;
			top: 40%;
			left: 50%;
			transform: translate(-50%, -50%);

			.title {
				text-align: center;
				font-size: 34rpx;
				font-weight: 500;
				color: #2E2E2E;
				margin-bottom: 48rpx;
			}

			.text {
				font-size: 26rpx;
				color: #333333;
				line-height: 1.5;
			}
		}

		.progress-box {
			position: absolute;
			width: 100%;
			box-sizing: border-box;
			bottom: 48rpx;
			width: 100%;
			padding: 0 48rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.progress-bg {
				width: calc(100% - 36rpx);
				height: 12rpx;
				border-radius: 12rpx;
				overflow: hidden;
				position: relative;
				background: #F7F8FA;

				.progress-cover {
					position: absolute;
					top: 0;
					left: 0;
					background: #00468C;
					height: 12rpx;
					border-radius: 4rpx 0 0 4rpx;
				}
			}

			.progress-text {
				margin-left: 24rpx;
				color: #333333;
				font-size: 22rpx;
			}
		}

		.update-btn {
			position: absolute;
			width: 100%;
			box-sizing: border-box;
			bottom: 24rpx;
			padding: 0 24rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 2.2;

			view {
				background: #00468C;
				border-radius: 8rpx;
			}
		}
	}
</style>
